package com.cqw.commontest;

import com.cqw.common.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserPrincipal;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-08-20 10:26
 * @Description:
 */
@RestController
@RequestMapping
public class TestController {

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/redis/error")
    public String test() {
        redisTemplate.opsForValue().set("key", "1", 10L, TimeUnit.SECONDS);
        return "OK";
    }

    @GetMapping("/redis/ok")
    public String testOk() {
        Object key = redisTemplate.opsForValue().get("key");
        return (String) key;
    }


    @GetMapping("testJackson")
    public BaseResponse testJackson() {
        JacksonUser user = new JacksonUser();
        user.setUserName("张三");
        user.setAge(12L);
        user.setUserId(1L);
        return BaseResponse.success(user);
    }

    @PostMapping("testJacksonD")
    public BaseResponse testJacksonD(@RequestBody JacksonUser user) {
        System.out.println(user);
        return BaseResponse.success(user);
    }


    public static void main(String[] args) throws IOException {
        Path path = Paths.get("D:\\自学技术资料\\JAVA基础面试题.md");
        UserPrincipal owner = Files.getOwner(path);
        String name = owner.getName();
        System.out.println(name);

        A:
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.println(j);
                if (j == 4) {
                    break A;
                }
            }
        }
    }



    @GetMapping("/redis1/list")
    public String list(@RequestParam("str") String str) {
        redisTemplate.opsForList().leftPush("list",str);

        List list = redisTemplate.opsForList().range("list", 0L, -1L);
        System.out.println(list);
        return null;
    }


}
